package com.entrogy.dao;

import com.entrogy.bean.UserEntity;
import com.entrogy.bean.VisitEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**  访客管理dao接口
 * @Author 许红松
 * @date 2019/6/27 0027 下午 3:25
 */
@Repository
public interface VisitMapper {
    // 根据家长访客获得历史记录的条数

    /**
     * 条件查询访客信息
     * @param map
     * @return
     */
    List<VisitEntity> getVisitEntityList(Map<String,Object> map);

    

    /**
     * 条件查询访客信息  用于分页查询总数
     * @param map
     * @return
     */
    int getListTotal(Map<String,Object> map);



    /**
     * 新增访客信息
     * @param visitEntity
     * @return
     */
    int addVisitEntity(VisitEntity visitEntity);

    /**
     *   根据id 查询详细信息
     * @return
     */
    Map<String,Object> selectVisitEntityById(int  id);


    /**
     *  审核
     * @param map
     * @return
     */
    int UpdateStype(Map<String,Object> map);



    /**
     *  获取班主任信息
     * @param gradeId
     * @return
     */
    UserEntity getUserId(@Param(value="gradeId") int  gradeId,@Param(value="schoolCode") int schoolCode);


    /**
     *  查询用户职位关联表信息
     * @param userId
     * @return
     */
    UserEntity getUserById(@Param(value="userId") String  userId);

    /**
     *   获取保安的list
     * @param map
     * @return
     */
    List<UserEntity> getSecurityList(Map<String,Object> map);

    /**
     *   老师查询的审核信息
     * @param map
     * @return
     */
    List<VisitEntity>  getTeacherVisit(Map<String,Object> map);

    /**
     *   老师查询的审核总数
     * @param map
     * @return
     */
    int  getTeacherVisitTotal(Map<String,Object> map);
    /* *//**
     *
     * @param uId
     * @return
     *
     */
    long getVisitBeanCountByPVId(String uId);
    /*throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT count(*) FROM visit_record WHERE parent_visitor_id=?";
        Object[] count = queryRunner.query(sql, new ArrayHandler(), uId);
        return (long) count[0];
    }*/

    // 根据老师id获得历史记录的条数
    long getVisitBeanCountByTeaId(String teaId);

    @Select("select * from visit_information where visit_id = #{id}")
    VisitEntity getVisitById(@Param("id") Integer id);

    /**
     * 查询访客开启状态
     * @return
     */
    Integer getVisitorEnableStatus();
   /* throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT count(*) FROM visit_record WHERE target_id=?";
        Object[] count = queryRunner.query(sql, new ArrayHandler(), teaId);
        return (long) count[0];
    }*/

    // 分页查询家长或访客的历史记录
  //  List<VisitBean> getVistBeanListByPagePV(String uId, int startIndex, int num);
    /*throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT * FROM visit_record WHERE parent_visitor_id=? ORDER BY isauthority,start_time DESC LIMIT ?,?";
        return queryRunner.query(sql, new BeanListHandler<VisitBean>(VisitBean.class), uId, startIndex, num);
    }*/

    // 分页查询老师的历史记录
 //   List<VisitBean> getVistBeanListByPageTea(String teaId, int startIndex, int num);
   /* throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT * FROM visit_record WHERE target_id=? ORDER BY isauthority,start_time DESC LIMIT ?,?";
        return queryRunner.query(sql, new BeanListHandler<VisitBean>(VisitBean.class), teaId, startIndex, num);
    }*/

    // 根据id获得家长访客的bean
   // ParentVisitorBean getParentVisitorBeanById(String uId);
    /*throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT * FROM parent_visitor_information WHERE parent_visitor_id=?";
        return queryRunner.query(sql, new BeanHandler<ParentVisitorBean>(ParentVisitorBean.class), uId);
    }*/

   /* StudentBean getMyKid(String uId, String kidName) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT * FROM student_information WHERE "
                + "(student_information.parent1_id=? OR student_information.parent2_id=?) AND " + "student_name=?";
        StudentBean stuBean = queryRunner.query(sql, new BeanHandler<StudentBean>(StudentBean.class), uId, uId,
                kidName);
        return stuBean;
    }

    // id找老师
    TeacherBean getTeacherBeanById(String tId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT * FROM teacher_information WHERE teacher_id=?";
        return queryRunner.query(sql, new BeanHandler<TeacherBean>(TeacherBean.class), tId);
    }

    // 找班主任
    TeacherBean getHeadmaster(String courseId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT * FROM teacher_information WHERE banzhuren=?";
        return queryRunner.query(sql, new BeanHandler<TeacherBean>(TeacherBean.class), courseId);
    }

    // 增加新的访问
    boolean insertNewVisit(String uId, String currentDateTime, String startTime, String endTime,
                                  String teacherId, String content) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "INSERT INTO visit_record"
                + "(parent_visitor_id,visit_time,start_time,end_time,target_id,visit_content,isauthority) VALUES"
                + "(?,?,?,?,?,?,0)";
        int i = queryRunner.update(sql, uId, currentDateTime, startTime, endTime, teacherId, content);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    // 更新家长访客的图片名字
    boolean updatePicName(String uId, String picName) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "UPDATE parent_visitor_information SET parent_visitor_base64=? WHERE parent_visitor_id=?";
        int i = queryRunner.update(sql, picName, uId);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    // 获得VisitBean
    VisitBean getVisitBean(String uId, int visitId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "SELECT * FROM visit_record WHERE visit_id=? AND target_id=?";
        return queryRunner.query(sql, new BeanHandler<VisitBean>(VisitBean.class), visitId, uId);
    }

    // 更新访问状态
    boolean updateVisit(int visitId, int isAuthority) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "UPDATE visit_record SET isauthority=? WHERE visit_id=?";
        int i = queryRunner.update(sql, isAuthority, visitId);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    // 更新家长访客的thdid
    boolean updatePVTHDId(String pvId, String thdId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "UPDATE parent_visitor_information SET parent_visitor_thdid=? WHERE parent_visitor_id=?";
        int i = queryRunner.update(sql, thdId, pvId);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    // 更新老师的thdid
    boolean updateTeaTHDId(String pvId, String thdId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "UPDATE teacher_information SET teacher_thdid=? WHERE teacher_id=?";
        int i = queryRunner.update(sql, thdId, pvId);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    // 更新学生thdid
    boolean updateStuTHDId(String pvId, String thdId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDatasource());
        String sql = "UPDATE student_information SET student_thdid=? WHERE id=?";
        int i = queryRunner.update(sql, thdId, pvId);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }*/

}
